package com.central.apps.dao.MaterialInventory;

import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface CheckDao {
    @Select("select es.applyby,es.subjectno,es.id,ry.itemnum,ry.itemdesc,es.curbal,st.avgcost,es.binnum,es.batchnum,es.taskid,es.proid from gq_invbalances es \n" +
            "            left join gq_inventory ry on es.storenum = ry.storenum and es.itemnum = ry.itemnum \n" +
            "            left join gq_invcost st on es.storenum = st.storenum and es.itemnum = st.itemnum and es.batchnum = st.batchnum  and es.proid =st.proid\n" +
            "            where ry.storenum = #{storenum} and ry.curbaltotal > 0 and es.curbal>0")
    List<Map<String, Object>> getKcpdzbList(String storenum);

    @Delete("delete from gq_kcpdzb where pidd = #{id}")
    void delKcpdzbList(String pidd);

    @Insert("insert into gq_kcpdzb(linecost,applyby,subjectno,invbalanid,ITEMNUM,ITEMNAME,Inventory,binnum,lotnum,curbalnum,unitcost,pidd,proid,taskid,checker,Invtime,delflag,checkqty,createuser,deptcode) " +
            "values(#{linecost},#{applyby},#{subjectno},#{id},#{itemnum},#{itemdesc},#{curbal},#{binnum},#{batchnum},#{curbal},#{avgcost},#{pidd},#{proid},#{taskid},#{checker},#{Invtime},#{delflag},#{checkqty},#{createuser},#{deptcode})")
    void insertKcpdzb(Map<String, Object> param);

    @Select("select zb.ITEMNUM,zb.ITEMNAME,zb.binnum,zb.lotnum,zb.curbalnum,zb.checkqty,zb.auditorqty,zb.differqty,zb.unitcost,t.kid,t.Pid " +
            "from gq_kcpdzb zb left join gq_check t on zb.pidd = t.id where zb.pidd = #{id}")
    List<Map<String, Object>> getKcpdzbByPidd(Long id);

    @Select("Select * from gq_kcpdzb where id = #{id}")
    Map<String, Object> getKcpdzbById(Long id);

    @Select("select ry.itemnum,ry.itemdesc,es.binnum,es.batchnum,es.curbal,st.avgcost from gq_invbalances es \n" +
            "left join gq_inventory ry on es.storenum = ry.storenum and es.itemnum = ry.itemnum \n" +
            "left join gq_invcost st on es.storenum = st.storenum and es.itemnum = st.itemnum and es.batchnum = st.batchnum\n" +
            "where ry.storenum = #{storenum} " +
            "and (ry.itemnum,es.binnum,es.batchnum) not in (select itemnum,binnum,lotnum from gq_kcpdzb where pidd = #{checkid})")
    List<Map<String, Object>> getInventoryByStorenum(String storenum, String checkid);

    @Update("update gq_invbalances set curbal = #{curbal} where itemnum = #{itemnum} and storenum = #{storenum} " +
            "and binnum = #{binnum} and batchnum = #{lotnum}")
    void updateInvbalances(Integer curbal, String itemnum, String storenum, String binnum, String lotnum);

    @Update("insert into gq_invadjustment(quantity,curbal,adjustby,adjustdate,remarks,storenum,itemnum) " +
            "values(#{quantity},#{curbal},#{adjustby},#{adjustdate},#{remarks},#{storenum},#{itemnum})")
    void insertInvadjustment(Map<String, Object> params);

    @Select("select * from gq_kcpdzb where pidd = #{pidd}")
    List<Map<String, Object>> getKcpdzbListById(String pidd);

    @Update("update gq_check set totalcost = #{totalcost},totalqty = #{totalqty} where id = #{id}")
    void updateCheck(String id, String totalcost, Integer totalqty);

    @Update("update gq_check set totalcost = (totalcost - #{linecost}), totalqty = (totalqty - #{differqty}) where id = #{id}")
    void updateCheckTotal(String id, Float linecost, Integer differqty);

}
